package leetCode.q6;

public class Solution {
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int len = s.length();
        /**
         1 向下 2 向右上
         */
        int orign = 1;

        /**
         3 4
         4 6
         5 8
         2n-2 一组
         */
        int col = (numRows-1)*len/(2*numRows-2)+numRows-1;

        char[][] mat = new char[numRows][col];
        int count=0,i=0,j=0;
        while(count<len){
            mat[i][j] = s.charAt(count);

            // change next point
            if(orign == 1){
                if(i+1<numRows){
                    i++;
                }else{
                    i--;
                    j++;
                    orign = 2;
                }
            }else if(orign == 2){
                if(i-1>=0){
                    i--;
                    j++;
                }else{
                    i++;
                    orign = 1;
                }
            }

            count++;
        }

        //get the matrix
        StringBuilder sb = new StringBuilder();
        for(i=0;i<numRows;i++){
            for(j=0;j<col;j++){
                if(mat[i][j] != 0){
                    sb.append(mat[i][j]);
                }
            }
        }
        return sb.toString();
    }
}
